package com.lord.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.lord.po.Image;
import com.lord.po.ImageMessage;
import com.lord.po.Music;
import com.lord.po.MusicMessage;
import com.lord.po.News;
import com.lord.po.NewsMessage;
import com.lord.po.TextMessage;
import com.thoughtworks.xstream.XStream;

/**
 * @Description: 微信消息管理
 * @author Lord
 * @date 2017-3-20 下午4:49:15
 */
public class MessageUtil {
	
	//文本消息
	public static final String MESSAGE_TEXT = "text";
	
	//图文消息
	public static final String MESSAGE_NEWS = "news";
	
	//图片消息
	public static final String MESSAGE_IMAGE = "image";
	
	//音乐消息
	public static final String MESSAGE_MUSIC = "music";
	
	//语音消息
	public static final String MESSAGE_VOICE = "voice";
	
	//视频消息
	public static final String MESSAGE_VIDEO = "video";
	
	//链接消息
	public static final String MESSAGE_LINK = "link";
	
	//地理位置消息
	public static final String MESSAGE_LOCATION = "location";
	
	//关注/取消关注事件
	public static final String MESSAGE_EVENT = "event";
	
	//用户未关注时，进行关注后的事件推送
	public static final String MESSAGE_SUBSCRIBE = "subscribe";
	
	//用户已关注时，进行取消关注的事件推送 
	public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe";
	
	//自定义菜单事件
	public static final String MESSAGE_CLICK = "CLICK";
	
	//点击菜单跳转链接时的事件推送
	public static final String MESSAGE_VIEW = "VIEW";
	
	//扫码推事件
	public static final String MESSAGE_SCANCODE= "scancode_push";

	
	/**
	 * @Title: xmlToMap
	 * @Description: xml转为map集合
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws DocumentException
	 * @author Lord
	 * @date 2017-3-20 下午5:06:50
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException{
		
		Map<String, String> map = new HashMap<String, String>();
		SAXReader reader = new SAXReader();
		//获取信息
		InputStream ins = request.getInputStream();
		Document doc = reader.read(ins);
		//获取根节点
		Element root = doc.getRootElement();
		
		List<Element> list = root.elements();
		for (Element e : list) {
			map.put(e.getName(), e.getText());
		}
		ins.close();
		return map;
	}

	/**
	 * @Title: textMsgToXml
	 * @Description: 文本消息对象转为xml
	 * @param textMessage
	 * @return
	 * @author Lord
	 * @date 2017-3-20 下午5:20:06
	 */
	public static String textMsgToXml(TextMessage textMessage){
		XStream xstream = new XStream();
		xstream.alias("xml", textMessage.getClass());
		return xstream.toXML(textMessage);
	}
	
	public static String initText(String toUserName, String fromUserName, String content){
		TextMessage text = new TextMessage();
		text.setFromUserName(toUserName);
		text.setToUserName(fromUserName);
		text.setMsgType(MessageUtil.MESSAGE_TEXT);
		text.setCreateTime(new Date().getTime());
		text.setContent(content);
		return textMsgToXml(text);
	}
	
	/**
	 * @Title: menuText
	 * @Description: 主菜单
	 * @return
	 * @author Lord
	 * @date 2017-3-20 下午9:38:26
	 */
	public static String menuText(){
		StringBuffer sbf = new StringBuffer();
		sbf.append("欢迎您的关注，请按照菜单提示进行操作：\n\n");
		sbf.append("1、课程介绍\n");
		sbf.append("2、慕课网介绍\n");
		sbf.append("3、每日一图\n");
		sbf.append("4、每日一听\n\n");
		sbf.append("回复?调查此菜单。");
		return sbf.toString();
	}
	
	public static String firstMenu(){
		StringBuffer sbf = new StringBuffer();
		sbf.append("本套课程介绍微信公众号开发，主要涉及公众号介绍、编辑模式介绍、开发模式介绍等");
		return sbf.toString();
	}
	
	public static String secondMenu(){
		StringBuffer sbf = new StringBuffer();
		sbf.append("慕课网是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里，你可以找到最好的互联网技术牛人，也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。");
		sbf.append("慕课网课程涵盖前端开发、PHP、Html5、Android、iOS、Swift等IT前沿技术语言，包括基础课程、实用案例、高级分享三大类型，适合不同阶段的学习人群。以纯干货、短视频的形式为平台特点，为在校学生、职场白领提供了一个迅速提升技能、共同分享进步的学习平台。");
		return sbf.toString();
	}
	
	public static String threeMenu(){
		StringBuffer sb = new StringBuffer();
		sb.append("词组翻译使用指南\n\n");
		sb.append("使用示例：\n");
		sb.append("翻译足球\n");
		sb.append("翻译中国足球\n");
		sb.append("翻译football\n\n");
		sb.append("回复？显示主菜单。");
		return sb.toString();
	}
	
	/**
	 * @Title: newsMsgToXml
	 * @Description: 图文消息对象转为xml
	 * @param newsMessage
	 * @return
	 * @author Lord
	 * @date 2017-3-20 下午5:20:06
	 */
	public static String newsMsgToXml(NewsMessage newsMessage){
		XStream xstream = new XStream();
		xstream.alias("xml", newsMessage.getClass());
		xstream.alias("item", new News().getClass());
		return xstream.toXML(newsMessage);
	}
	
	/**
	 * @Title: initNews
	 * @Description: 图文消息的组装
	 * @param toUserName
	 * @param fromUserName
	 * @return
	 * @author Lord
	 * @date 2017-3-21 下午8:59:20
	 */
	public static String initNews(String toUserName, String fromUserName){
		String message = null;
		List<News> newsList = new ArrayList<News>();
		NewsMessage newsMessage = new NewsMessage();
		
		News news = new News();
		news.setTitle("慕课网介绍");
		news.setDescription("慕课网是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里，你可以找到最好的互联网技术牛人，也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。");
		news.setPicUrl("http://lord.tunnel.qydev.com/WeChat/image/weixin.gif");
		news.setUrl("www.imooc.com");
		
		newsList.add(news);
		
		newsMessage.setToUserName(fromUserName);
		newsMessage.setFromUserName(toUserName);
		newsMessage.setCreateTime(new Date().getTime());
		newsMessage.setMsgType(MessageUtil.MESSAGE_NEWS);
		newsMessage.setArticles(newsList);
		newsMessage.setArticleCount(newsList.size());
		
		message = newsMsgToXml(newsMessage);
		return message;
	}
	
	/**
	 * @Title: imageMsgToXml
	 * @Description: 图片消息对象转为xml
	 * @param imageMessage
	 * @return
	 * @author Lord
	 * @date 2017-3-21 下午10:44:45
	 */
	public static String imageMsgToXml(ImageMessage imageMessage){
		XStream xstream = new XStream();
		xstream.alias("xml", imageMessage.getClass());
		return xstream.toXML(imageMessage);
	}
	
	/**
	 * @Title: initImage
	 * @Description: 图片消息的组装
	 * @param toUserName
	 * @param fromUserName
	 * @return
	 * @author Lord
	 * @date 2017-3-21 下午10:42:23
	 */
	public static String initImage(String toUserName, String fromUserName){
		String message = null;
		Image image = new Image();
		image.setMediaId("wDq8-5kVV8kuWOZAWOr6PIZlcq7gcAVgYDpQzx_hPu48623sLbMXgguUZIDoLJFh");
		
		ImageMessage imageMessage = new ImageMessage();
		imageMessage.setFromUserName(toUserName);
		imageMessage.setToUserName(fromUserName);
		imageMessage.setMsgType(MessageUtil.MESSAGE_IMAGE);
		imageMessage.setCreateTime(new Date().getTime());
		imageMessage.setImage(image);
		
		message = imageMsgToXml(imageMessage);
		return message;
	}
	
	/**
	 * @Title: musicMsgToXml
	 * @Description: 音乐消息对象转为xml
	 * @param musicMessage
	 * @return
	 * @author Lord
	 * @date 2017-3-26 下午10:44:45
	 */
	public static String musicMsgToXml(MusicMessage musicMessage){
		XStream xstream = new XStream();
		xstream.alias("xml", musicMessage.getClass());
		return xstream.toXML(musicMessage);
	}
	
	/**
	 * @Title: initMusic
	 * @Description: 音乐消息的组装
	 * @param toUserName
	 * @param fromUserName
	 * @return
	 * @author Lord 
	 * @date 2017-3-26 下午4:31:37
	 */
	public static String initMusic(String toUserName, String fromUserName){
		String message = null;
		Music music = new Music();
		music.setThumbMediaId("bBDm9-K57u4BvLHbWt3Ki9Lbq5FhzUlKsjzllDGTaV_LBu57C5cObZ-zhYc3jGmH");
		music.setTitle("see you again");
		music.setDescription("速7片尾曲");
		music.setMusicUrl("http://119.29.235.162/WeChat/music/SeeYouAgain.mp3");
		music.setHQMusicUrl("http://119.29.235.162/WeChat/music/SeeYouAgain.mp3");
		
		MusicMessage musicMessage = new MusicMessage();
		musicMessage.setFromUserName(toUserName);
		musicMessage.setToUserName(fromUserName);
		musicMessage.setMsgType(MessageUtil.MESSAGE_MUSIC);
		musicMessage.setCreateTime(new Date().getTime());
		musicMessage.setMusic(music);  
		
		message = musicMsgToXml(musicMessage);
		return message;
	}
}
